package com.yinhe.music.yhmusic.dialog.pay;

import com.hwangjr.rxbus.RxBus;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.IBaseModel;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.network.response.ResponseTransformer;

public class PayDialogPresenter extends BasePresenter<IBaseModel, IPayDialogContract.IPayDialogView> implements IPayDialogContract.IPayDialogPresenter {
    @Override
    public void getProductInfo(String songId) {
        if (getView() == null) {
            return;
        }

        addSubscription(mModel.getProductInfo("song", songId)
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(productInfo -> {
                    getView().setPayDialogUI(productInfo);
                }, throwable -> {
                    getView().showMessage(throwable);
                }));
    }

    @Override
    public void buyMusicBySnowGold(String product) {
        if (getView() == null) {
            return;
        }

        addSubscription(mModel.getPayInfo(IConstants.SNOW_GOLD, IConstants.PRODUCT_SONG, product)
                .compose(schedulerProvider.applySchedulers())
                .subscribe(payInfoResponse -> {
                    RxBus.get().post(RxBusEventType.Music.BUY_MUSIC, product);
                }, throwable -> {
                    getView().showMessage(throwable);
                }));
    }
}
